package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.RelationsDAO;
import org.gap.dao.RelationsTiersDAO;
import org.gap.dao.TiersDAO;

import org.gap.domain.Relations;
import org.gap.domain.RelationsTiers;
import org.gap.domain.Tiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for RelationsTiers entities
 * 
 */

@Service("RelationsTiersService")
@Transactional
public class RelationsTiersServiceImpl implements RelationsTiersService {

	/**
	 * DAO injected by Spring that manages Relations entities
	 * 
	 */
	@Autowired
	private RelationsDAO relationsDAO;

	/**
	 * DAO injected by Spring that manages RelationsTiers entities
	 * 
	 */
	@Autowired
	private RelationsTiersDAO relationsTiersDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * Instantiates a new RelationsTiersServiceImpl.
	 *
	 */
	public RelationsTiersServiceImpl() {
	}

	/**
	 */
	@Transactional
	public RelationsTiers findRelationsTiersByPrimaryKey(Integer rltTiersParent, Integer rltTiersFils, String rltRelation) {
		return relationsTiersDAO.findRelationsTiersByPrimaryKey(rltTiersParent, rltTiersFils, rltRelation);
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public RelationsTiers saveRelationsTiersTiersByRltTiersFils(Integer rltTiersParent, Integer rltTiersFils, String rltRelation, Tiers related_tiersbyrlttiersfils) {
		RelationsTiers relationstiers = relationsTiersDAO.findRelationsTiersByPrimaryKey(rltTiersParent, rltTiersFils, rltRelation, -1, -1);
		Tiers existingtiersByRltTiersFils = tiersDAO.findTiersByPrimaryKey(related_tiersbyrlttiersfils.getTieTiers());

		// copy into the existing record to preserve existing relationships
		if (existingtiersByRltTiersFils != null) {
			existingtiersByRltTiersFils.setTieTiers(related_tiersbyrlttiersfils.getTieTiers());
			existingtiersByRltTiersFils.setTieIdentClient(related_tiersbyrlttiersfils.getTieIdentClient());
			existingtiersByRltTiersFils.setTieTypeTiers(related_tiersbyrlttiersfils.getTieTypeTiers());
			existingtiersByRltTiersFils.setTieLibExterne(related_tiersbyrlttiersfils.getTieLibExterne());
			existingtiersByRltTiersFils.setTieNationalite(related_tiersbyrlttiersfils.getTieNationalite());
			existingtiersByRltTiersFils.setTieTelFix(related_tiersbyrlttiersfils.getTieTelFix());
			existingtiersByRltTiersFils.setTieTelBureau(related_tiersbyrlttiersfils.getTieTelBureau());
			existingtiersByRltTiersFils.setTieTelPortable(related_tiersbyrlttiersfils.getTieTelPortable());
			existingtiersByRltTiersFils.setTieFax(related_tiersbyrlttiersfils.getTieFax());
			existingtiersByRltTiersFils.setTieEMail(related_tiersbyrlttiersfils.getTieEMail());
			existingtiersByRltTiersFils.setTieDateCreation(related_tiersbyrlttiersfils.getTieDateCreation());
			existingtiersByRltTiersFils.setTieUserCreation(related_tiersbyrlttiersfils.getTieUserCreation());
			related_tiersbyrlttiersfils = existingtiersByRltTiersFils;
		}

		relationstiers.setTiersByRltTiersFils(related_tiersbyrlttiersfils);
		related_tiersbyrlttiersfils.getRelationsTiersesForRltTiersFils().add(relationstiers);
		relationstiers = relationsTiersDAO.store(relationstiers);
		relationsTiersDAO.flush();

		related_tiersbyrlttiersfils = tiersDAO.store(related_tiersbyrlttiersfils);
		tiersDAO.flush();

		return relationstiers;
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public RelationsTiers deleteRelationsTiersTiersByRltTiersParent(Integer relationstiers_rltTiersParent, Integer relationstiers_rltTiersFils, String relationstiers_rltRelation, Integer related_tiersbyrlttiersparent_tieTiers) {
		RelationsTiers relationstiers = relationsTiersDAO.findRelationsTiersByPrimaryKey(relationstiers_rltTiersParent, relationstiers_rltTiersFils, relationstiers_rltRelation, -1, -1);
		Tiers related_tiersbyrlttiersparent = tiersDAO.findTiersByPrimaryKey(related_tiersbyrlttiersparent_tieTiers, -1, -1);

		relationstiers.setTiersByRltTiersParent(null);
		related_tiersbyrlttiersparent.getRelationsTiersesForRltTiersParent().remove(relationstiers);
		relationstiers = relationsTiersDAO.store(relationstiers);
		relationsTiersDAO.flush();

		related_tiersbyrlttiersparent = tiersDAO.store(related_tiersbyrlttiersparent);
		tiersDAO.flush();

		tiersDAO.remove(related_tiersbyrlttiersparent);
		tiersDAO.flush();

		return relationstiers;
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public RelationsTiers saveRelationsTiersTiersByRltTiersParent(Integer rltTiersParent, Integer rltTiersFils, String rltRelation, Tiers related_tiersbyrlttiersparent) {
		RelationsTiers relationstiers = relationsTiersDAO.findRelationsTiersByPrimaryKey(rltTiersParent, rltTiersFils, rltRelation, -1, -1);
		Tiers existingtiersByRltTiersParent = tiersDAO.findTiersByPrimaryKey(related_tiersbyrlttiersparent.getTieTiers());

		// copy into the existing record to preserve existing relationships
		if (existingtiersByRltTiersParent != null) {
			existingtiersByRltTiersParent.setTieTiers(related_tiersbyrlttiersparent.getTieTiers());
			existingtiersByRltTiersParent.setTieIdentClient(related_tiersbyrlttiersparent.getTieIdentClient());
			existingtiersByRltTiersParent.setTieTypeTiers(related_tiersbyrlttiersparent.getTieTypeTiers());
			existingtiersByRltTiersParent.setTieLibExterne(related_tiersbyrlttiersparent.getTieLibExterne());
			existingtiersByRltTiersParent.setTieNationalite(related_tiersbyrlttiersparent.getTieNationalite());
			existingtiersByRltTiersParent.setTieTelFix(related_tiersbyrlttiersparent.getTieTelFix());
			existingtiersByRltTiersParent.setTieTelBureau(related_tiersbyrlttiersparent.getTieTelBureau());
			existingtiersByRltTiersParent.setTieTelPortable(related_tiersbyrlttiersparent.getTieTelPortable());
			existingtiersByRltTiersParent.setTieFax(related_tiersbyrlttiersparent.getTieFax());
			existingtiersByRltTiersParent.setTieEMail(related_tiersbyrlttiersparent.getTieEMail());
			existingtiersByRltTiersParent.setTieDateCreation(related_tiersbyrlttiersparent.getTieDateCreation());
			existingtiersByRltTiersParent.setTieUserCreation(related_tiersbyrlttiersparent.getTieUserCreation());
			related_tiersbyrlttiersparent = existingtiersByRltTiersParent;
		}

		relationstiers.setTiersByRltTiersParent(related_tiersbyrlttiersparent);
		related_tiersbyrlttiersparent.getRelationsTiersesForRltTiersParent().add(relationstiers);
		relationstiers = relationsTiersDAO.store(relationstiers);
		relationsTiersDAO.flush();

		related_tiersbyrlttiersparent = tiersDAO.store(related_tiersbyrlttiersparent);
		tiersDAO.flush();

		return relationstiers;
	}

	/**
	 * Delete an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public void deleteRelationsTiers(RelationsTiers relationstiers) {
		relationsTiersDAO.remove(relationstiers);
		relationsTiersDAO.flush();
	}

	/**
	 * Return a count of all RelationsTiers entity
	 * 
	 */
	@Transactional
	public Integer countRelationsTierss() {
		return ((Long) relationsTiersDAO.createQuerySingleResult("select count(*) from RelationsTiers o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public void saveRelationsTiers(RelationsTiers relationstiers) {
		RelationsTiers existingRelationsTiers = relationsTiersDAO.findRelationsTiersByPrimaryKey(relationstiers.getRltTiersParent(), relationstiers.getRltTiersFils(), relationstiers.getRltRelation());

		if (existingRelationsTiers != null) {
			if (existingRelationsTiers != relationstiers) {
				existingRelationsTiers.setRltTiersParent(relationstiers.getRltTiersParent());
				existingRelationsTiers.setRltTiersFils(relationstiers.getRltTiersFils());
				existingRelationsTiers.setRltRelation(relationstiers.getRltRelation());
				existingRelationsTiers.setRltSens(relationstiers.getRltSens());
				existingRelationsTiers.setRltDateDebut(relationstiers.getRltDateDebut());
				existingRelationsTiers.setRltDateFin(relationstiers.getRltDateFin());
			}
			relationstiers = relationsTiersDAO.store(existingRelationsTiers);
		} else {
			relationstiers = relationsTiersDAO.store(relationstiers);
		}
		relationsTiersDAO.flush();
	}

	/**
	 * Load an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public Set<RelationsTiers> loadRelationsTierss() {
		return relationsTiersDAO.findAllRelationsTierss();
	}

	/**
	 * Save an existing Relations entity
	 * 
	 */
	@Transactional
	public RelationsTiers saveRelationsTiersRelations(Integer rltTiersParent, Integer rltTiersFils, String rltRelation, Relations related_relations) {
		RelationsTiers relationstiers = relationsTiersDAO.findRelationsTiersByPrimaryKey(rltTiersParent, rltTiersFils, rltRelation, -1, -1);
		Relations existingrelations = relationsDAO.findRelationsByPrimaryKey(related_relations.getRelRelation());

		// copy into the existing record to preserve existing relationships
		if (existingrelations != null) {
			existingrelations.setRelRelation(related_relations.getRelRelation());
			existingrelations.setRelLibelleDesc(related_relations.getRelLibelleDesc());
			existingrelations.setRelLibelleAsc(related_relations.getRelLibelleAsc());
			existingrelations.setRelType(related_relations.getRelType());
			related_relations = existingrelations;
		}

		relationstiers.setRelations(related_relations);
		related_relations.getRelationsTierses().add(relationstiers);
		relationstiers = relationsTiersDAO.store(relationstiers);
		relationsTiersDAO.flush();

		related_relations = relationsDAO.store(related_relations);
		relationsDAO.flush();

		return relationstiers;
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public RelationsTiers deleteRelationsTiersTiersByRltTiersFils(Integer relationstiers_rltTiersParent, Integer relationstiers_rltTiersFils, String relationstiers_rltRelation, Integer related_tiersbyrlttiersfils_tieTiers) {
		RelationsTiers relationstiers = relationsTiersDAO.findRelationsTiersByPrimaryKey(relationstiers_rltTiersParent, relationstiers_rltTiersFils, relationstiers_rltRelation, -1, -1);
		Tiers related_tiersbyrlttiersfils = tiersDAO.findTiersByPrimaryKey(related_tiersbyrlttiersfils_tieTiers, -1, -1);

		relationstiers.setTiersByRltTiersFils(null);
		related_tiersbyrlttiersfils.getRelationsTiersesForRltTiersFils().remove(relationstiers);
		relationstiers = relationsTiersDAO.store(relationstiers);
		relationsTiersDAO.flush();

		related_tiersbyrlttiersfils = tiersDAO.store(related_tiersbyrlttiersfils);
		tiersDAO.flush();

		tiersDAO.remove(related_tiersbyrlttiersfils);
		tiersDAO.flush();

		return relationstiers;
	}

	/**
	 * Return all RelationsTiers entity
	 * 
	 */
	@Transactional
	public List<RelationsTiers> findAllRelationsTierss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<RelationsTiers>(relationsTiersDAO.findAllRelationsTierss(startResult, maxRows));
	}

	/**
	 * Delete an existing Relations entity
	 * 
	 */
	@Transactional
	public RelationsTiers deleteRelationsTiersRelations(Integer relationstiers_rltTiersParent, Integer relationstiers_rltTiersFils, String relationstiers_rltRelation, String related_relations_relRelation) {
		RelationsTiers relationstiers = relationsTiersDAO.findRelationsTiersByPrimaryKey(relationstiers_rltTiersParent, relationstiers_rltTiersFils, relationstiers_rltRelation, -1, -1);
		Relations related_relations = relationsDAO.findRelationsByPrimaryKey(related_relations_relRelation, -1, -1);

		relationstiers.setRelations(null);
		related_relations.getRelationsTierses().remove(relationstiers);
		relationstiers = relationsTiersDAO.store(relationstiers);
		relationsTiersDAO.flush();

		related_relations = relationsDAO.store(related_relations);
		relationsDAO.flush();

		relationsDAO.remove(related_relations);
		relationsDAO.flush();

		return relationstiers;
	}
}
